package de.gwtprime.client.views;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.ErrorWidget;
import de.gwtprime.client.util.GWTUtils;
import de.gwtprime.client.views.popup.EndOfDayPopup;
import de.gwtprime.client.views.popup.ImprovementsPopup;
import de.gwtprime.client.views.popup.NoEmotionsPopup;
import de.gwtprime.client.views.popup.PerformancePopup;
import de.gwtprime.client.widgets.ArticlePreviewWidgetImpl;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.request.ArticleRequest;

public class WelcomeViewImpl extends Composite implements WelcomeView {

	private Presenter presenter;
	public static WelcomeViewImplUiBinder uiBinder = GWT.create(WelcomeViewImplUiBinder.class);
	interface WelcomeViewImplUiBinder extends UiBinder<Widget, WelcomeViewImpl> {}

	ClientFactory clientFactory;
	
	String name;
	
	@UiField Label noEmotionsTitle;
	@UiField Label performanceTitle;
	@UiField Label endOfDayTitle;
	@UiField Label improvementsTitle;
	
	@UiField Button readMoreSupportBtn;
	@UiField Button readMorePrideFolioBtn;
	@UiField Button readMoreTimeRespectBtn;
	@UiField Button readMoreBestQualityBtn;
	
	@UiField VerticalPanel articlesPanel;
	
	public WelcomeViewImpl(){
		
		initWidget(uiBinder.createAndBindUi(this));
		readMoreSupportBtn.setText(GWTUtils.I18n.readMore());
		readMorePrideFolioBtn.setText(GWTUtils.I18n.readMore());
		readMoreTimeRespectBtn.setText(GWTUtils.I18n.readMore());
		readMoreBestQualityBtn.setText(GWTUtils.I18n.readMore());
		
		noEmotionsTitle.setText(GWTUtils.I18n.noEmotionsTitle());
		performanceTitle.setText(GWTUtils.I18n.performanceTitle());
		endOfDayTitle.setText(GWTUtils.I18n.endOfDayTitle());
		improvementsTitle.setText(GWTUtils.I18n.improvementsTitle());
		
		requestFactoryTest();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	/**
	 * Open support popup
	 * @param e
	 */
	@UiHandler("readMoreSupportBtn")
	public void openMoreSupport(ClickEvent e){
		
		PopupPanel popup = new PopupPanel(true);
		popup.add(new NoEmotionsPopup());
		popup.setWidth(AppUtils.popupWidth);
		popup.center();
	}
	
	/**
	 * Open support popup
	 * @param e
	 */
	@UiHandler("readMorePrideFolioBtn")
	public void openPrideFolio(ClickEvent e){
		
		PopupPanel popup = new PopupPanel(true);
		popup.add(new PerformancePopup());
		popup.setWidth(AppUtils.popupWidth);
		popup.center();
		
	}
	
	/**
	 * Open time respect popup.
	 * @param e
	 */
	@UiHandler("readMoreTimeRespectBtn")
	public void openTimeRespect(ClickEvent e){
		
		PopupPanel popup = new PopupPanel(true);
		popup.add(new EndOfDayPopup());
		popup.setWidth(AppUtils.popupWidth);
		popup.center();
		
	}
	
	/**
	 * Open support popup
	 * @param e
	 */
	@UiHandler("readMoreBestQualityBtn")
	public void openBestPerformance(ClickEvent e){
		
		PopupPanel popup = new PopupPanel(true);
		popup.add(new ImprovementsPopup());
		popup.setWidth(AppUtils.popupWidth);
		popup.center();
		
	}
	
	public Presenter getPresenter(){
		return presenter;
	}

	@Override
	public void setName(String name) {
		this.name=name;
	}

	@Override
	public void setClientFactory(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}
	
	private void requestFactoryTest() {
		final EventBus eventBus = new SimpleEventBus();
		final PrimeRequestFactory requestFactory = GWT.create(PrimeRequestFactory.class);
		requestFactory.initialize(eventBus);
		
		requestFactory.initialize(eventBus);
		
		final ArticleRequest request2 = requestFactory.articleRequest();
		
		Request<List<ArticleProxy>> createReq2 = request2.loadLatestArticles(5).with("sections");
		
		articlesPanel.add(new ErrorWidget("Artikel werden geladen..."));
		
		createReq2.fire(new Receiver<List<ArticleProxy>>()
				{
				  @Override
				    public void onSuccess(List<ArticleProxy> list)
				    {
					  	articlesPanel.clear();
					    AppUtils.log("Successfully loaded latest articles");
				        for(final ArticleProxy article : list){
				        	articlesPanel.add(new ArticlePreviewWidgetImpl(clientFactory, article));
				       }
				    }

				  @Override
				  public void onFailure(ServerFailure error) {
					  articlesPanel.clear();
					  articlesPanel.add(new ErrorWidget("Artikel konnten nicht geladen werden"));
					  AppUtils.log("Failed to load articles");
					}
				});
	}
}
